package helpers;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import entities.Page;

public class CrawlerHelper {
	public static Page getContent(String url) {
		Page page = new Page();
		String content = "";
		try {
			Document doc = Jsoup.connect(url).get();
			content = doc.select("#tpsummary").html();
			content = content.replace("http://www.nlm.nih.gov", "");
			
			String title = doc.select("#tp_title").html();
			String sortTitle = doc.select("#ht_alsocalled").html();
			String image = doc.select("#topicimage_new img").attr("src");
			
			Elements elms = doc.select("#starthere li a");
			if (!elms.html().isEmpty()) {
				for (Element elm : elms) {
					if (page.getArticle().isEmpty()) {
						if (elm.attr("href").contains("www.nhlbi.nih.gov")) {
							String article = CrawlerHelper.getArticleNhlbi(elm.attr("href"));
							page.setArticle(article);
						} 
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.ninds.nih.gov")) {
							String article = CrawlerHelper.getArticleNinds(elm.attr("href"));
							page.setArticle(article);
						} 
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.niams.nih.gov")) {
							String article = CrawlerHelper.getArticleNiams(elm.attr("href"));
							page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.flu.gov")) {
							String article = CrawlerHelper.getArticleFluGov(elm.attr("href"));
							page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.niaaa.nih.gov")) {
							String article = CrawlerHelper.getArticleNiaaa(elm.attr("href"));
							page.setArticle(article);
						}

						if (page.getArticle().isEmpty() && elm.attr("href").contains("digestive.niddk.nih.gov")) {
							String article = CrawlerHelper.getArticleNiddk(elm.attr("href"));
							page.setArticle(article);
						} 
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("familydoctor.org")) {
							String article = CrawlerHelper.getArticleFamilyDoctor(elm.attr("href"));
							page.setArticle(article);
						} 
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("nihseniorhealth.gov")) {
							String article = CrawlerHelper.getArticleNihseniorhealth(elm.attr("href"));
							page.setArticle(article);
						} 
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.skinsight.com")) {
							String article = CrawlerHelper.getArticleSkinsight(elm.attr("href"));
							page.setArticle(article);
						} 
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.nichd.nih.gov")) {
							String article = CrawlerHelper.getArticleNichd(elm.attr("href"));
							page.setArticle(article);
						} 
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("kidshealth.org")) {
							String article = CrawlerHelper.getArticleKidshealth(elm.attr("href"));
							page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.cdc.gov")) {
							String article = CrawlerHelper.getArticleCdcgov(elm.attr("href"));
							page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.healthinaging.org")) {
							String article = CrawlerHelper.getArticleHealthingaging(elm.attr("href"));
							page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.nia.nih.gov")) {
								String article = CrawlerHelper.getArticleNia(elm.attr("href"));
								page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("orthoinfo.aaos.org")) {
							String article = CrawlerHelper.getArticleOrthoinfo(elm.attr("href"));
							page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("endocrine.niddk.nih.gov")) {
							String article = CrawlerHelper.getArticleEndocrine(elm.attr("href"));
							page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.cancer.gov")) {
							String article = CrawlerHelper.getArticleCancer(elm.attr("href"));
							page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("ods.od.nih.gov")) {
							String article = CrawlerHelper.getArticleOds(elm.attr("href"));
							page.setArticle(article);
						}
						
						if (page.getArticle().isEmpty() && elm.attr("href").contains("www.hormone.org")) {
							String article = CrawlerHelper.getArticleHormone(elm.attr("href"));
							page.setArticle(article);
						}
					}
				}
			}
			
			page.setTitle(title);
			page.setSortTitle(sortTitle);
			page.setContent(content);
			page.setImage(image);
			page.setUrl(url);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return page;
	}
	
	public static Page getContentTopic(String url) {
		Page page = new Page();
		
		String content = "";
		try {
			Document doc = Jsoup.connect(url).get();
			content = doc.select("#ht_content .ht_topiclisting").html();
			content = content.replace("http://www.nlm.nih.gov", "");
			
			String title = doc.select("#ht_title").html();
			
			page.setTitle(title);
			page.setContent(content);
			page.setUrl(url);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return page;
	}
	
	public static String getArticleNhlbi(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).get();
			doc.select("#DCIThreeMainColumn .DCIaddthis, #DCIThreeMainColumn #rateThisContainerOuter").remove();
			content = doc.select("#DCIThreeMainColumn").html();
			content = content.replace("http://www.nhlbi.nih.gov", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleNinds(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).get();
			content = doc.select("#readspeaker").html();
			content = content.replace("http://www.ninds.nih.gov", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleNiams(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).get();
			doc.select("#column2 ul.pubDocs, #bottom, #column2 dl, #column2 p:contains(1 AMS Circle)").remove();
			content = doc.select("#column2").html();
			content = content.replace("http://www.niams.nih.gov", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleNiaaa(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).get();
			content = "<h1>" + doc.select("#page-title").html() + "</h1>";
			content = content + doc.select("#block-system-main .main-content").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleFluGov(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).get();
			content = doc.select("#content .syndicate").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleNiddk(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).get();
			doc.select("#breadcrumb").remove();
			content = doc.select("#content").html();
			content = content.replaceAll("[<]{1}(img)[^>]+[>]{1}", "").replaceAll("<\\/img>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleFamilyDoctor(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).get();
			doc.select("#textResizeControl, .headerimg, .prevpagelink").remove();
			content = doc.select("#header").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
			content = content.replace("src=\"/", "src=\"http://familydoctor.org/");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleNihseniorhealth(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			doc.select("#content noscript, #content .thumbright").remove();
			content = doc.select("#content").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleSkinsight(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			doc.select("#content .directionButtonContainer").remove();
			content = "<h2>" + doc.select("#titleArea h1").text() + "</h2>";
			content += doc.select("#content .dxText").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleNichd(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			//doc.select("#content .directionButtonContainer").remove();
			content = doc.select("#nichd_main_content_content_left_center_content").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleKidshealth(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			doc.select("#whichRead_1 .ISI_MESSAGE, #whichRead_1 readspeaker_button1, #readspeaker_button1").remove();
			content = doc.select("#whichRead_1").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
			content = content.replaceAll("[<]{1}(img)[^>]+[>]{1}", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleCdcgov(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			doc.select("#whichRead_1 .ISI_MESSAGE, #whichRead_1 readspeaker_button1, #readspeaker_button1").remove();
			content = "<h1>" + doc.select("#topic-title") + "</h1>";
			content += doc.select("#bottom-container .subColumns .c75l").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleHealthingaging(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			doc.select("#whichRead_1 p:contains(Printer-friendly), #whichRead_1 p:contains(Get more information)").remove();
			content = doc.select("#right .user-content").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleNia(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			//doc.select("").remove();
			content = "<h1>" + doc.select("#page-title") + "</h1>";
			content += doc.select(".content .pane-content .field-item").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleOrthoinfo(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			doc.select(".copyrightTop, #rightColumn, .pageTop, .copyrightBox, .rightColumn, " +
					".partnerBox, .disclaimer, .copyrightBottom, .linkBarPrint, #addressBlock, #footer").remove();
			content = "<h1>" + doc.select(".shell .contentCell .topicTitle") + "</h1>";
			content += doc.select(".shell .contentCell ").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleEndocrine(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			doc.select("#breadcrumb").remove();
			content = doc.select("#content ").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleCancer(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			//doc.select("#breadcrumb").remove();
			content = doc.select("#cgvBody .slot-item").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
			content = content.replace("src=\"/", "src=\"http://www.cancer.gov/");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleOds(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			//doc.select("#breadcrumb").remove();
			content = doc.select("#content").html();
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return content;
	}
	
	public static String getArticleHormone(String url) {
		String content = "";
		try {
			Document doc = Jsoup.connect(url).followRedirects(true).get();
			//doc.select("#breadcrumb").remove();
			content = "<h1>" + doc.select("#cs_control_552 .CS_Textblock_Text h2").html() + "</h1>";
			content += doc.select("#cs_control_574").html();
			
			content = content.replaceAll("[<]{1}(a)[^>]+[>]{1}", "").replaceAll("<\\/a>", "");
		} catch (Exception e) {
			
		}
		
		return content;
	}
	
	public static List<String> getUrlSitemap() {
		List<String> urls = new ArrayList<String>();
		
		List<String> sources = new ArrayList<String>();
		sources.add("http://www.nlm.nih.gov/medlineplus/healthtopics_a.html");
		sources.add("http://www.nlm.nih.gov/medlineplus/healthtopics_b.html");
		sources.add("http://www.nlm.nih.gov/medlineplus/healthtopics_c.html");
		
		try {
			for (int j = 0; j < sources.size(); j++) {
				Document doc = Jsoup.connect(sources.get(j)).get();
				Elements links = doc.select("#ht_content .ht_topiclisting ul li a");
				for (int i = 0; i < links.size(); i++) {
					if (urls.size() < 500) {
						String url = links.get(i).attr("href");
						url = url.replace("http://www.nlm.nih.gov/", "");
						urls.add(url);
					}
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		return urls;
	}
	
	public static void main(String[] args) {
		String url = "http://nihseniorhealth.gov/takingmedicines/toc.html";
		
	}
}
